<?php

abstract class Vega_Cache_Engine_Abstract
{
    protected $_prefix = '';

    protected $_options = array();

    abstract public function __construct($options);


    public static function validateId($name)
    {
        if (empty($name))
        {
            require_once 'Vega/Cache/Engine/Exception.php';
            throw new Vega_Cache_Engine_Exception('The id can not be null');
        }
        
        if (!preg_match('~^[\w]+$~D', $name))
        {
            require_once 'Vega/Cache/Engine/Exception.php';
            throw new Vega_Cache_Engine_Exception("Invalid id '$name' : must use only [a-zA-Z0-9_]");
        }
        return true;
    }
    
    /**
     * Returns cache id
     *
     * @param mixed $name
     * @return string
     */
    public function getId($name)
    {
        self::validateId($name);
        return "{$this->_prefix}$name";
    }
    
    /**
     * Sets cache prefix
     *
     * @param mixed $prefix
     * @return Vega_Cache_Engine_Abstract
     */
    public function setPrefix($prefix)
    {
        $this->_prefix = "$prefix";
        return $this;
    }
    
    /**
     * Returns cache prefix
     *
     * @return string
     */
    public function getPrefix()
    {
        return $this->_prefix;
    }
    
    abstract public function save($name, $data, $lifetime = null);
    
    abstract public function load($name);
    
    abstract public function delete($name);

    abstract public function isExpired($name, $lifetime = null);
}
